(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'));
  } else {
    root.Varint = factory(root.KaitaiStream);
  }
}(typeof self !== 'undefined' ? self : this, function (KaitaiStream, Varint) {
  var Varint = (function() {
    function Varint(_io, _parent, _root) {
      this._io = _io;
      this._parent = _parent;
      this._root = _root || this;

      this.size = 0
      this.value = 0;  
      this.buf = []

      this._read();
    }
    Varint.prototype._read = function() {
      let shift = 0;
    
      while (shift <= 70) {
        this.size += 1;
        let byte = this._io.readBytes(1)[0];
        this.buf.push(byte);

        // 检查最高位（符号位，但在varint中用作继续位）  
        if ((byte & 0x80) === 0) {  
            // 如果没有设置最高位，表示这是最后一个字节  
            this.value |= (byte & 0x7F) << shift;  
            break;  
        } else {  
            // 设置当前字节的7位到结果中，并准备读取下一个字节  
            this.value |= (byte & 0x7F) << shift;  
            shift += 7;  
        }
      }
    }

    return Varint;
  })();
  return Varint;
}));
